package com.zbec.mdrs.service;

import com.zbec.framework.core.service.SystemDictionaryService;
import com.zbec.framework.security.entity.SysOrganization;
import com.zbec.framework.security.service.SecurityService;
import com.zbec.mdrs.constants.Constants;
import com.zbec.mdrs.entity.Logs;
import com.zbec.mdrs.entity.RecordEntity;
import com.zbec.mdrs.entity.discount.Discount;
import com.zbec.mdrs.entity.drafts.Drafts;
import com.zbec.mdrs.entity.entrustedLoan.EntrustedLoan;
import com.zbec.mdrs.entity.guarantee.Guarantee;
import com.zbec.mdrs.entity.history.History;
import com.zbec.mdrs.entity.loan.Loan;
import com.zbec.mdrs.repository.jpa.*;
import com.zbec.mdrs.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * Created by fxl on 2015/10/19.
 */
@Component
@Transactional
public class MbtService {


    @Autowired
    private SecurityService securityService;

    @Autowired
    private HistoryDao historyDao;

    @Autowired
    private LoanDao loanDao;

    @Autowired
    private DiscountDao discountDao;

    @Autowired
    private DraftsDao draftsDao;

    @Autowired
    private GuaranteeDao guaranteeDao;

    @Autowired
    private EntrustedLoanDao entrustedLoanDao;

    @Autowired
    private LogsService logsService;

    @Autowired
    private MbtDao mbtDao;

    @Autowired
    private SystemDictCacheService systemDictCacheService;

    @Autowired
    private ValidationService validationService;


    public String exportMBTDataByMonth(String year, String month, String[] bizChecked, Boolean restrainId,String userIp) {
        String beginDate = year + month + "01";
        String endDate = year + month + "31";
        String dataDate = year + month;
//        Map mbtMap = findAllDataFromMBT(beginDate, endDate, restrainId);

        Long orgId= securityService.getCurrentUserVO().getBranchId();

        String message="";
        if(orgId!=null) {
            SysOrganization organization= securityService.getOrganizationByOrgId(orgId);


            for (int i = 0; i < bizChecked.length; i++) {
                String x = bizChecked[i];

                if (Constants.BUSINESS_TYPE_LOAN.equals(x)) {
                    Long loanHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_LOAN);
                    dealBusinessObject(loanHistoryId, new Loan(),organization);
//                saveBillFromMBT((List) mbtMap.get("bill"), dataDate,userIp);

                    List<LinkedCaseInsensitiveMap> list = mbtDao.findBillDataFromMBT(beginDate, endDate, restrainId, organization);
                    if (list.size() > 0) {
                        saveBillFromMBT(list, dataDate, userIp, Constants.BUSINESS_TYPE_LOAN);
                        message = message + ResourceUtils.getLocaleString("message.23.exportsuccess")+"===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.23.noexportdata")+"===";
                    }

                } else if (Constants.BUSINESS_TYPE_DISCOUNT.equals(x)) {
                    Long discountHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_DISCOUNT);
                    dealBusinessObject(discountHistoryId, new Discount(),organization);

                    List<LinkedCaseInsensitiveMap> list = mbtDao.findDiscountDataFromMBT(beginDate, endDate, restrainId, organization);
                    if (list.size() > 0) {
                        saveDiscountFromMBT(list, dataDate, userIp, Constants.BUSINESS_TYPE_DISCOUNT);
                        message = message + ResourceUtils.getLocaleString("message.24.exportsuccess")+"===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.24.noexportdata")+"===";
                    }

                } else if (Constants.BUSINESS_TYPE_DRAFTS.equals(x)) {
                    Long draftsHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_DRAFTS);
                    dealBusinessObject(draftsHistoryId, new Drafts(),organization);

                    List<LinkedCaseInsensitiveMap> list = mbtDao.findDraftsFromMBT(beginDate, endDate, restrainId, organization);
                    if (list.size() > 0) {
                        saveDraftsFromMBT(list, dataDate, userIp, Constants.BUSINESS_TYPE_DRAFTS);
                        message = message + ResourceUtils.getLocaleString("message.25.exportsuccess")+"===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.25.noexportdata")+"===";
                    }


                } else if (Constants.BUSINESS_TYPE_ENTRUSTED_LOAN.equals(x)) {
                    Long entrustedLoanHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_ENTRUSTED_LOAN);
                    dealBusinessObject(entrustedLoanHistoryId, new EntrustedLoan(),organization);

                    List<LinkedCaseInsensitiveMap> list = mbtDao.findEntrustedLoanFromMBT(beginDate, endDate, restrainId, organization);
                    if (list.size() > 0) {
                        saveEntrustedLoanFromMBT(list, dataDate, userIp, Constants.BUSINESS_TYPE_ENTRUSTED_LOAN);
                        message = message + ResourceUtils.getLocaleString("message.26.exportsuccess")+"===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.26.noexportdata")+"===";
                    }
                } else if (Constants.BUSINESS_TYPE_GUARANTEE.equals(x)) {
                    Long guaranteeHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_GUARANTEE);
                    dealBusinessObject(guaranteeHistoryId, new Guarantee(),organization);

                    List<LinkedCaseInsensitiveMap> list = mbtDao.findGuaranteeFromMBT(beginDate, endDate, restrainId, organization);
                    if (list.size() > 0) {
                        savGuaranteeFromMBT(list, dataDate, userIp, Constants.BUSINESS_TYPE_GUARANTEE);
                        message = message + ResourceUtils.getLocaleString("message.27.exportsuccess")+"===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.27.noexportdata")+"===";
                    }

                } else if (Constants.BUSINESS_TYPE_FINANCE.equals(x)) {
                    Long guaranteeHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_FINANCE);
                    dealBusinessObject(guaranteeHistoryId, new Loan(),organization);

                    saveBillFromMBT(mbtDao.findFinanceDataFromMBT(beginDate, endDate, restrainId, organization), dataDate, userIp, Constants.BUSINESS_TYPE_FINANCE);
                } else if (Constants.BUSINESS_TYPE_BAOLI.equals(x)) {
                    Long guaranteeHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_BAOLI);
                    dealBusinessObject(guaranteeHistoryId, new Loan(),organization);

                    saveBillFromMBT(mbtDao.findBaoliDataFromMBT(beginDate, endDate, restrainId, organization), dataDate, userIp, Constants.BUSINESS_TYPE_BAOLI);

                }
            }
        }else {
            message = message + ResourceUtils.getLocaleString("message.organnull")+"===";
        }
        return message;
    }

    private void saveEntrustedLoanFromMBT(List<LinkedCaseInsensitiveMap> billList, String dataDate,String userIp,String buizType) {
        Set<EntrustedLoan> entrustedLoanSet = new HashSet<EntrustedLoan>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED,buizType, dataDate);
        historyDao.save(history);
        String maxFny = entrustedLoanDao.getMaxEntrustedLoanFny(dataDate);
        int i = 0;
        for (LinkedCaseInsensitiveMap m : billList) {
            ++i;
            EntrustedLoan entrustedLoanBean = new EntrustedLoan();
            Long recordId = Long.parseLong(m.get("ID").toString());
            String zzjgdm=m.get("orgcode").toString();
            if (m.get("Money_kind") != null)
                entrustedLoanBean.setBz(Constants.CURRENCY_MAP.get(m.get("money_kind").toString()));
            if (m.get("Bill_money") != null)
                entrustedLoanBean.setYwfse(new BigDecimal(m.get("Bill_money").toString()));
            if (m.get("Bill_ye") != null)
                entrustedLoanBean.setYwye(new BigDecimal(m.get("Bill_ye").toString()));
            if (m.get("Create_date") != null)
                entrustedLoanBean.setDkffrq(new Date(((java.sql.Date) m.get("Create_date")).getTime()));
            if (m.get("Maturity_date") != null)
                entrustedLoanBean.setDkdqrq(new Date(((java.sql.Date) m.get("Maturity_date")).getTime()));
            if (m.get("card_no") != null)
                entrustedLoanBean.setDkkbm(m.get("card_no").toString());
            if (m.get("name_cn") != null)
                entrustedLoanBean.setQymc(m.get("name_cn").toString());
            if (m.get("bill_no") != null)
                entrustedLoanBean.setJjh(m.get("bill_no").toString());

            entrustedLoanBean.setRecordId(recordId);

            EntrustedLoan entrustedLoanDrmap = entrustedLoanDao.getEntrustedLoanFromDrmap(recordId, maxFny);


            if(entrustedLoanDrmap!=null){
                if(entrustedLoanDrmap.getMissedField()!=null)
                    entrustedLoanBean.setMissedField(entrustedLoanDrmap.getMissedField());
                if(entrustedLoanDrmap.getErrorDesc()!=null)
                    entrustedLoanBean.setErrorDesc(entrustedLoanDrmap.getErrorDesc());
                if(entrustedLoanDrmap.getExistError()!=null)
                    entrustedLoanBean.setExistError(entrustedLoanDrmap.getExistError());
                if(entrustedLoanDrmap.getWtrzzjgdm()!=null)
                    entrustedLoanBean.setWtrzzjgdm(entrustedLoanDrmap.getWtrzzjgdm());
                if(entrustedLoanDrmap.getJkrzzjgdm()!=null)
                    entrustedLoanBean.setJkrzzjgdm(entrustedLoanDrmap.getJkrzzjgdm());
            }

            SysOrganization org= securityService.getOrganizationByZzjgdm(zzjgdm);
            if(org!=null){
                entrustedLoanBean.setJrjgbm(org.getOrgCode());
                entrustedLoanBean.setOrgId(org.getId());
            }
            entrustedLoanBean.setSource(Constants.SOURCE_MBT);
            entrustedLoanBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            entrustedLoanBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            entrustedLoanBean.setSjsj(dataDate);

            ValidationUtils.validateData(Constants.BUSINESS_TYPE_ENTRUSTED_LOAN, entrustedLoanBean);

            entrustedLoanBean.setHistory(history);

            entrustedLoanDao.save(entrustedLoanBean);
            entrustedLoanSet.add(entrustedLoanBean);

            logsService.handleBuzLogData(userIp, "logs.type.26", "logs.operation.import",true,entrustedLoanBean.getDetail());
        }

        history.setRecordNumber((long) i);
        history.setEntrustedLoanSet(entrustedLoanSet);
    }

    private void savGuaranteeFromMBT(List<LinkedCaseInsensitiveMap> billList, String dataDate,String userIp,String buizType) {
        Set<Guarantee> guaranteeSet = new HashSet<Guarantee>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED,buizType, dataDate);
        historyDao.save(history);
        String maxFny = guaranteeDao.getMaxGuaranteeFny(dataDate);
        int i = 0;
        for (LinkedCaseInsensitiveMap m : billList) {
            ++i;
            Guarantee guaranteeBean = new Guarantee();
            Long recordId = Long.parseLong(m.get("ID").toString());
            String zzjgdm=m.get("orgcode").toString();

            if (m.get("Baohan_no") != null)
                guaranteeBean.setDbhth(m.get("Baohan_no").toString());
            if (m.get("Money_kind") != null)
                guaranteeBean.setBz(Constants.CURRENCY_MAP.get(m.get("money_kind").toString()));
            if (m.get("Baohan_kind") != null)
                guaranteeBean.setDbywzl(m.get("Baohan_kind").toString());
            if (m.get("Baohan_balance") != null)
                guaranteeBean.setDbye(new BigDecimal(m.get("Baohan_balance").toString()));
            if (m.get("Start_date") != null)
                guaranteeBean.setDbksrq(new Date(((java.sql.Date) m.get("Start_date")).getTime()));

            if (m.get("End_date") != null) {
                try {
                    Date date = DateUtils.parseDate(m.get("End_date").toString(), DateUtils.DATE_FORMAT_YYYYMMDD);
                    guaranteeBean.setDbdqrq(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            if (m.get("Five_class") != null)
                guaranteeBean.setWjfl(m.get("Five_class").toString());
            if (m.get("Baohan_money") != null)
                guaranteeBean.setDydbfsje(new BigDecimal(m.get("Baohan_money").toString()));
            if (m.get("card_no") != null)
                guaranteeBean.setDkkbm(m.get("card_no").toString());
            if (m.get("currentCnt") != null)
                guaranteeBean.setDyfsbs(parseDoubleString2Integer(m.get("currentCnt").toString()));
            if (m.get("currentSum") != null)
                guaranteeBean.setDydbfsje(new BigDecimal(m.get("currentSum").toString()));
            if (m.get("totalCnt") != null)
                guaranteeBean.setZbbs(parseDoubleString2Integer(m.get("totalCnt").toString()) );

            guaranteeBean.setRecordId(recordId);

            Guarantee guaranteeDrmap = guaranteeDao.getGuaranteeFromDrmap(recordId, maxFny);


            if(guaranteeDrmap!=null){
                if(guaranteeDrmap.getMissedField()!=null)
                    guaranteeBean.setMissedField(guaranteeDrmap.getMissedField());
                if(guaranteeDrmap.getErrorDesc()!=null)
                    guaranteeBean.setErrorDesc(guaranteeDrmap.getErrorDesc());
                if(guaranteeDrmap.getExistError()!=null)
                    guaranteeBean.setExistError(guaranteeDrmap.getExistError());
                if(guaranteeDrmap.getBdbrzzjgdm()!=null)
                    guaranteeBean.setBdbrzzjgdm(guaranteeDrmap.getBdbrzzjgdm());
                if(guaranteeDrmap.getDydcfsje()!=null)
                    guaranteeBean.setDydcfsje(guaranteeDrmap.getDydcfsje());
            }

            SysOrganization org= securityService.getOrganizationByZzjgdm(zzjgdm);
            if(org!=null){
                guaranteeBean.setJrjgbm(org.getOrgCode());
                guaranteeBean.setOrgId(org.getId());
            }

            guaranteeBean.setSource(Constants.SOURCE_MBT);
            guaranteeBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            guaranteeBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            guaranteeBean.setSjsj(dataDate);
            guaranteeBean.setDbjgzzdm(zzjgdm);

            ValidationUtils.validateData(Constants.BUSINESS_TYPE_GUARANTEE, guaranteeBean);

            guaranteeBean.setHistory(history);

            guaranteeDao.save(guaranteeBean);
            guaranteeSet.add(guaranteeBean);

            logsService.handleBuzLogData(userIp, "logs.type.27", "logs.operation.import",true,guaranteeBean.getDetail());
        }

        history.setRecordNumber((long) i);
        history.setGuaranteedSet(guaranteeSet);
    }


    private void saveDiscountFromMBT(List<LinkedCaseInsensitiveMap> billList, String dataDate,String userIp,String buizType) {
        Set<Discount> discountSet = new HashSet<Discount>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED, buizType, dataDate);
        historyDao.save(history);
        int i = 0;
        String maxFny = discountDao.getMaxDiscountFny(dataDate);
        for (LinkedCaseInsensitiveMap m : billList) {
            ++i;
            Discount discountBean = new Discount();
            Long recordId = Long.parseLong(m.get("id").toString());

            String zzjgdm=m.get("orgcode").toString();

            if (m.get("certify_code") != null)
                discountBean.setZzjgdm(m.get("certify_code").toString());
            if (m.get("Proposer_name") != null)
                discountBean.setTxsqrmc(m.get("Proposer_name").toString());
            if (m.get("Bill_kind") != null)
                discountBean.setPjzl(m.get("Bill_kind").toString());
            if (m.get("Acceptor_name") != null)
                discountBean.setCdhmc(m.get("Acceptor_name").toString());
            if (m.get("Money_kind") != null)
                discountBean.setBz(Constants.CURRENCY_MAP.get(m.get("Money_kind").toString()));
//            if (m.get("Four_class") != null)
//                discountBean.setFsjfl(m.get("Four_class").toString());
            if (m.get("Five_class") != null)
                discountBean.setWjfl(m.get("Five_class").toString());
            if (m.get("Discount_money") != null)
                discountBean.setTxje(new BigDecimal(m.get("Discount_money").toString()));
            if (m.get("Bill_money") != null)
                discountBean.setPmje(new BigDecimal(m.get("Bill_money").toString()));
            if (m.get("Discount_date") != null)
                discountBean.setTxrq(new Date(((java.sql.Date) m.get("Discount_date")).getTime()));
            if (m.get("Accept_date") != null)
                discountBean.setCdrq(new Date(((java.sql.Date) m.get("Accept_date")).getTime()));
            if (m.get("card_no") != null)
                discountBean.setDkkbm(m.get("card_no").toString());
            if (m.get("bill_no") != null)
                discountBean.setPjnbbh(m.get("bill_no").toString());
            discountBean.setRecordId(recordId);

            Discount discountDrmap = discountDao.getDiscountFromDrmap(recordId,maxFny);
            if (discountDrmap != null) {
                if(discountDrmap.getTxll()!=null)
                    discountBean.setTxll(discountDrmap.getTxll());
                if(discountDrmap.getZzjgdm()!=null)
                    discountBean.setZzjgdm(discountDrmap.getZzjgdm());
                if(discountDrmap.getZsmy()!=null)
                    discountBean.setZsmy(discountDrmap.getZsmy());
                if(discountDrmap.getZsrmb()!=null)
                    discountBean.setZsrmb(discountDrmap.getZsrmb());
                if(discountDrmap.getMissedField()!=null)
                    discountBean.setMissedField(discountDrmap.getMissedField());
                if(discountDrmap.getErrorDesc()!=null)
                    discountBean.setErrorDesc(discountDrmap.getErrorDesc());
                if(discountDrmap.getExistError()!=null)
                    discountBean.setExistError(discountDrmap.getExistError());
            }

            SysOrganization org= securityService.getOrganizationByZzjgdm(zzjgdm);
            if(org!=null){
                discountBean.setJrjgbm(org.getOrgCode());
                discountBean.setWdssqu(org.getField02());
                discountBean.setOrgId(org.getId());
                discountBean.setJrjgwdmc(org.getOrgName());
            }

            discountBean.setSource(Constants.SOURCE_MBT);
            discountBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            discountBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            discountBean.setSjsj(dataDate);


            ValidationUtils.validateData(Constants.BUSINESS_TYPE_DISCOUNT, discountBean);

            discountBean.setHistory(history);

            discountDao.save(discountBean);
            discountSet.add(discountBean);

            logsService.handleBuzLogData(userIp,"logs.type.24","logs.operation.import",true,discountBean.getDetail());
        }
        history.setRecordNumber((long) i);
        history.setDiscountSet(discountSet);
    }

    private void saveDraftsFromMBT(List<LinkedCaseInsensitiveMap> billList, String dataDate,String userIp,String buizType) {
        Set<Drafts> draftsSet = new HashSet<Drafts>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED,buizType, dataDate);
        historyDao.save(history);
        String maxFny = draftsDao.getMaxDraftsFny(dataDate);
        int i = 0;
        for (LinkedCaseInsensitiveMap m : billList) {
            ++i;
            Drafts draftsBean = new Drafts();
            Long recordId = Long.parseLong(m.get("ID").toString());
            String zzjgdm=m.get("orgcode").toString();
            if (m.get("Remitter_name") != null)
                draftsBean.setCprmc(m.get("Remitter_name").toString());
            if (m.get("Money_kind") != null)
                draftsBean.setBz(Constants.CURRENCY_MAP.get(m.get("money_kind").toString()));
            if (m.get("Bill_money") != null)
                draftsBean.setHpje(new BigDecimal(m.get("Bill_money").toString()));
            if (m.get("Accept_date") != null)
                draftsBean.setHpcdr(new Date(((java.sql.Date) m.get("Accept_date")).getTime()));
            if (m.get("Maturity_date") != null)
                draftsBean.setHpdqr(new Date(((java.sql.Date) m.get("Maturity_date")).getTime()));
            if (m.get("Pay_date") != null)
                draftsBean.setHpfkrq(new Date(((java.sql.Date) m.get("Pay_date")).getTime()));
            if (m.get("Deposit_scale") != null)
                draftsBean.setBzjbl(new BigDecimal( m.get("Deposit_scale").toString()));
//            if (m.get("Assure_flag") != null)
//                draftsBean.setDbbz(m.get("Assure_flag").toString());
            if (m.get("diankuanflag") != null)
                draftsBean.setDkbz(m.get("diankuanflag").toString());
            if (m.get("Five_class") != null)
                draftsBean.setWjfl(m.get("Five_class").toString());
            if (m.get("card_no") != null)
                draftsBean.setDkkbm(m.get("card_no").toString());
            if (m.get("bill_no") != null)
                draftsBean.setCdxyh(m.get("bill_no").toString());
            draftsBean.setRecordId(recordId);

            Drafts draftsDrmap = draftsDao.getDraftsFromDrmap(recordId,maxFny);


            if(draftsDrmap!=null){
                if(draftsDrmap.getMissedField()!=null)
                    draftsBean.setMissedField(draftsDrmap.getMissedField());
                if(draftsDrmap.getErrorDesc()!=null)
                    draftsBean.setErrorDesc(draftsDrmap.getErrorDesc());
                if(draftsDrmap.getExistError()!=null)
                    draftsBean.setExistError(draftsDrmap.getExistError());
                if(draftsDrmap.getZzjgdm()!=null)
                    draftsBean.setZzjgdm(draftsDrmap.getZzjgdm());
            }

            SysOrganization org= securityService.getOrganizationByZzjgdm(zzjgdm);
            if(org!=null){
                draftsBean.setJrjgbm(org.getOrgCode());
                draftsBean.setOrgId(org.getId());
            }

            draftsBean.setSource(Constants.SOURCE_MBT);
            draftsBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            draftsBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            draftsBean.setSjsj(dataDate);


            ValidationUtils.validateData(Constants.BUSINESS_TYPE_DRAFTS, draftsBean);

            draftsBean.setHistory(history);

            draftsDao.save(draftsBean);
            draftsSet.add(draftsBean);

            logsService.handleBuzLogData(userIp,"logs.type.25","logs.operation.import",true,draftsBean.getDetail());
        }

        history.setRecordNumber((long) i);
        history.setDraftsdSet(draftsSet);
    }


    private void saveBillFromMBT(List<LinkedCaseInsensitiveMap> billList, String dataDate,String userIp,String buizType) {
        Set<Loan> loanSet = new HashSet<Loan>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED,buizType, dataDate);
        historyDao.save(history);
        String maxFny = loanDao.getMaxLoanFny(dataDate);
        int i = 0;
        for (LinkedCaseInsensitiveMap m : billList) {
            ++i;
            Loan loanBean = new Loan();
            Long recordId = Long.parseLong(m.get("id").toString());
            String zzjgdm=m.get("orgcode").toString();
            if (m.get("loanCont_no") != null)
                loanBean.setDkhth(m.get("loanCont_no").toString());
            if (m.get("ensure_money") != null)
                loanBean.setBzje(new BigDecimal(m.get("ensure_money").toString()));

            if (m.get("ensure_desc") != null) {
                loanBean.setBzmx(m.get("ensure_desc").toString());
            }
            if (m.get("impawn_money") != null)
                loanBean.setZyje(new BigDecimal(m.get("impawn_money").toString()));
            if (m.get("impawn_desc") != null) {
                System.out.println("before detail=====" + m.get("name_cn").toString() + "###" + m.get("impawn_desc"));
                loanBean.setZymx(m.get("impawn_desc").toString());
            }
            if (m.get("pledge_money") != null)
                loanBean.setDyje(new BigDecimal(m.get("pledge_money").toString()));
            if (m.get("pledge_desc") != null)
                loanBean.setDymx(m.get("pledge_desc").toString());
            if (m.get("certify_code") != null)
                loanBean.setZzjgdm(m.get("certify_code").toString());
            if (m.get("name_cn") != null)
                loanBean.setQymc(m.get("name_cn").toString());
            if (m.get("card_no") != null)
                loanBean.setDkkbm(m.get("card_no").toString());
            if (m.get("bill_no") != null)
                loanBean.setDkjjh(m.get("bill_no").toString());
            if (m.get("money_kind") != null)
                loanBean.setBz(Constants.CURRENCY_MAP.get(m.get("money_kind").toString()));
            if (m.get("Loan_kind") != null)
                loanBean.setDklb(m.get("Loan_kind").toString());//
            if (m.get("dbfs") != null)
                loanBean.setDbfs(m.get("dbfs").toString());
            if (m.get("Five_class") != null)
                loanBean.setWjfl(m.get("Five_class").toString());
            if (m.get("Bill_money") != null)
                loanBean.setDkffje(new BigDecimal(m.get("Bill_money").toString()) );
            if (m.get("Bill_ye") != null)
                loanBean.setDkye(new BigDecimal(m.get("Bill_ye").toString()));
            if (m.get("create_date") != null)
                loanBean.setFkrq(new Date(((java.sql.Date) m.get("create_date")).getTime()));
            if (m.get("Maturity_date") != null)
                loanBean.setDqrq(new Date(((java.sql.Date) m.get("Maturity_date")).getTime()));
            if (m.get("end_date") != null)
                loanBean.setZqrq(new Date(((java.sql.Date) m.get("end_date")).getTime()));
            loanBean.setRecordId(recordId);

            Loan loanDrmap=null;
            List<Loan> loanList = loanDao.getLoanFromDrmap(recordId,maxFny);
            if(loanList!=null && loanList.size()>0){
                loanDrmap=loanList.get(0);
            }
            if(loanDrmap!=null){
                if(loanDrmap.getZzjgdm()!=null)
                    loanBean.setZzjgdm(loanDrmap.getZzjgdm());
                if(loanDrmap.getQymc()!=null)
                    loanBean.setQymc(loanDrmap.getQymc());
                if(loanDrmap.getDkkbm()!=null)
                    loanBean.setDkkbm(loanDrmap.getDkkbm());
                if(loanDrmap.getNbpj()!=null)
                    loanBean.setNbpj(loanDrmap.getNbpj());
                if(loanDrmap.getWbpj()!=null)
                    loanBean.setWbpj(loanDrmap.getWbpj());
                if(loanDrmap.getJbll()!=null)
                    loanBean.setJbll(loanDrmap.getJbll());
                if(loanDrmap.getLlfd()!=null)
                    loanBean.setLlfd(loanDrmap.getLlfd());
                if(loanDrmap.getSjll()!=null)
                    loanBean.setSjll(loanDrmap.getSjll());
                if(loanDrmap.getDbfs()!=null)
                    loanBean.setDbfs(loanDrmap.getDbfs());
                if(loanDrmap.getQxje()!=null)
                    loanBean.setQxje(loanDrmap.getQxje());
                if(loanDrmap.getMissedField()!=null)
                    loanBean.setMissedField(loanDrmap.getMissedField());
                if(loanDrmap.getErrorDesc()!=null)
                    loanBean.setErrorDesc(loanDrmap.getErrorDesc());
                if(loanDrmap.getExistError()!=null)
                    loanBean.setExistError(loanDrmap.getExistError());
            }

            SysOrganization org= securityService.getOrganizationByZzjgdm(zzjgdm);
            if(org!=null){
                loanBean.setJrjgwdmc(org.getOrgName());
                loanBean.setJrjgbm(org.getOrgCode());
                loanBean.setWdssqu(org.getField02());
                loanBean.setOrgId(org.getId());
            }
            loanBean.setSource(Constants.SOURCE_MBT);
            if(loanBean.getDkye()!=null && loanBean.getDkye().intValue()==0){
                loanBean.setRecordStatus(Constants.RECORD_STATUS_ADDEDSKIPED);
            }else {
                loanBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            }

            loanBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            loanBean.setSjsj(dataDate);

            ValidationUtils.validateData(Constants.BUSINESS_TYPE_LOAN, loanBean);

            loanBean.setHistory(history);
            loanDao.save(loanBean);
            loanSet.add(loanBean);
            logsService.handleBuzLogData(userIp, "logs.type.23", "logs.operation.import", true, loanBean.getDetail());
        }

        history.setRecordNumber((long) i);
        history.setLoanSet(loanSet);
    }

    private History generateHistory(String status, String type, String fny) {
        History histBean = new History();
        histBean.setRecordStatus(status);
        histBean.setBusinessType(type);
        histBean.setBusinessMonth(fny);
        histBean.setImportDate(new Date());

        histBean.setImportBy(this.securityService.getCurrentUser().getLoginName());
        return histBean;
    }

    private void dealBusinessObject(Long historyId, RecordEntity entity,SysOrganization organization) {
        Boolean isFh=false;
        if(organization.getOrgType().equals(Constants.BANK_TYPE_FH)){
            isFh=true;
        }
        if (entity instanceof Loan) {
            List<Loan> loanList=null;
            if(isFh){
                loanList = loanDao.findByHistoryId(historyId);
            }else{
                loanList =  loanDao.findByHistoryIdAndOrgId(historyId, organization.getId());
            }
            for (Loan loanBean : loanList) {
                loanBean.setRecordStatus(Constants.RECORD_STATUS_DELETED);
            }
            this.loanDao.save(loanList);
        }

        if (entity instanceof Discount) {
            List<Discount> discountList=null;
            if(isFh){
                discountList = discountDao.findByHistoryId(historyId);
            }else{
                discountList =  discountDao.findByHistoryIdAndOrgId(historyId, organization.getId());
            }
            for (Discount discountBean : discountList) {
                discountBean.setRecordStatus(Constants.RECORD_STATUS_DELETED);
            }
            this.discountDao.save(discountList);
        }

        if (entity instanceof Drafts) {
            List<Drafts> draftsList=null;
            if(isFh){
                draftsList = draftsDao.findByHistoryId(historyId);
            }else{
                draftsList =  draftsDao.findByHistoryIdAndOrgId(historyId, organization.getId());
            }
            for (Drafts draftsBean : draftsList) {
                draftsBean.setRecordStatus(Constants.RECORD_STATUS_DELETED);
            }
            this.draftsDao.save(draftsList);
        }

        if (entity instanceof EntrustedLoan) {
            List<EntrustedLoan> entrustedLoanList=null;
            if(isFh){
                entrustedLoanList = entrustedLoanDao.findByHistoryId(historyId);
            }else{
                entrustedLoanList =  entrustedLoanDao.findByHistoryIdAndOrgId(historyId, organization.getId());
            }
            for (EntrustedLoan entrustedLoanBean : entrustedLoanList) {
                entrustedLoanBean.setRecordStatus(Constants.RECORD_STATUS_DELETED);
            }
            this.entrustedLoanDao.save(entrustedLoanList);
        }

        if (entity instanceof Guarantee) {
            List<Guarantee> guaranteeList=null;
            if(isFh){
                guaranteeList = guaranteeDao.findByHistoryId(historyId);
            }else{
                guaranteeList =  guaranteeDao.findByHistoryIdAndOrgId(historyId, organization.getId());
            }
            for (Guarantee guaranteeBean : guaranteeList) {
                guaranteeBean.setRecordStatus(Constants.RECORD_STATUS_DELETED);
            }
            this.guaranteeDao.save(guaranteeList);
        }
    }

    private Long dealHistory(String dataDate, String businessType) {
        Long HistoryId = (long) 0;
        List<History> historyList = historyDao.findByBusinessMonthAndRecordStatusAndBusinessType(dataDate,Constants.RECORD_STATUS_ADDED,businessType);
        if (historyList != null && historyList.size() != 0) {
            for (History historyBean : historyList) {
                historyBean.setRecordStatus(Constants.RECORD_STATUS_DELETED);
                historyDao.save(historyBean);
                HistoryId = historyBean.getId();
            }
        }

        return HistoryId;
    }

    private Integer parseDoubleString2Integer(String doubleData){
        String[] dateArr = doubleData.split("\\.");
        if(dateArr.length>0){
            return Integer.parseInt(dateArr[0]);
        }
        return 0;
    }

    private Map<Object, Map> readFile(MultipartFile file,Integer dataColumCount){
        Map<Object, Map> map=null;
        ReaderExcel excelReader = null;
        ReaderTxt txtReader=null;
        String fileName = file.getOriginalFilename();
        System.out.println("excel start:==="+System.currentTimeMillis());
        if (fileName.endsWith(".txt")) {
            txtReader=new ReaderTxt();
            map = txtReader.readTxtContent(file,dataColumCount);
        }else {
            excelReader = new ReaderExcel();
            map = excelReader.readExcelContent(file,dataColumCount);
        }
        System.out.println("excel end:==="+System.currentTimeMillis());
        return map;
    }

    @Transactional(rollbackFor=Exception.class)
    public String mbtImport(MultipartFile file, String userIp, String tempType)throws ParseException,BusinessException {
        String dataDate ="";
        Long orgId= securityService.getCurrentUserVO().getBranchId();
        String message="";
        if(orgId!=null) {
            SysOrganization organization = securityService.getOrganizationByOrgId(orgId);
            Map<Object, Map> map=null;
            if (Constants.BUSINESS_TYPE_LOAN.equals(tempType)) {
                map= readFile(file,2);
                dataDate=map.get("dataMap").get("dataDate").toString();
               if(StringUtils.isEmpty(dataDate) || dataDate.length()!=6){
                   if(dataDate.equals("数据时间")){
                       message = message + ResourceUtils.getLocaleString("message.modelerror") + "===";
                       throw new BusinessException(message);
                   }
                   if(dataDate.length()!=6) {
                       message = message + ResourceUtils.getLocaleString("message.dateerror") + "===";
                       throw new BusinessException(message);
                   }
               }else{
                   Long loanHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_LOAN);
                   dealBusinessObject(loanHistoryId, new Loan(),organization);
                   if (map.size() > 0) {
                       convetLoan(map,dataDate,userIp,tempType);
                       message = message + ResourceUtils.getLocaleString("message.23.exportsuccess") + "===";
                   }else{
                       message = message + ResourceUtils.getLocaleString("message.23.noexportdata") + "===";
                   }
               }

            } else if (Constants.BUSINESS_TYPE_DISCOUNT.equals(tempType)){
                map= readFile(file,1);
                dataDate=map.get("dataMap").get("dataDate").toString();
                if(StringUtils.isEmpty(dataDate) || dataDate.length()!=6){
                    if(dataDate.equals("数据时间")){
                        message = message + ResourceUtils.getLocaleString("message.modelerror") + "===";
                        throw new BusinessException(message);
                    }
                    if(dataDate.length()!=6) {
                        message = message + ResourceUtils.getLocaleString("message.dateerror") + "===";
                        throw new BusinessException(message);
                    }
                }else {
                    Long discountHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_DISCOUNT);
                    dealBusinessObject(discountHistoryId, new Discount(), organization);
                    if (map.size() > 0) {
                        convetDiscount(map, dataDate, userIp, tempType);
                        message = message + ResourceUtils.getLocaleString("message.24.exportsuccess") + "===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.24.noexportdata") + "===";
                    }
                }

            }else if (Constants.BUSINESS_TYPE_DRAFTS.equals(tempType)){
                map= readFile(file,1);
                dataDate=map.get("dataMap").get("dataDate").toString();

                if(StringUtils.isEmpty(dataDate) || dataDate.length()!=6){
                    if(dataDate.equals("数据时间")){
                        message = message + ResourceUtils.getLocaleString("message.modelerror") + "===";
                        throw new BusinessException(message);
                    }
                    if(dataDate.length()!=6) {
                        message = message + ResourceUtils.getLocaleString("message.dateerror") + "===";
                        throw new BusinessException(message);
                    }
                }else {
                    Long draftsHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_DRAFTS);
                    dealBusinessObject(draftsHistoryId, new Drafts(), organization);

                    if (map.size() > 0) {
                        convetDrafts(map, dataDate, userIp, tempType);
                        message = message + ResourceUtils.getLocaleString("message.25.exportsuccess") + "===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.25.noexportdata") + "===";
                    }
                }
            }else if (Constants.BUSINESS_TYPE_ENTRUSTED_LOAN.equals(tempType)){
                map= readFile(file,0);
                dataDate=map.get("dataMap").get("dataDate").toString();

                if(StringUtils.isEmpty(dataDate) || dataDate.length()!=6){
                    if(dataDate.equals("数据时间")){
                        message = message + ResourceUtils.getLocaleString("message.modelerror") + "===";
                        throw new BusinessException(message);
                    }
                    if(dataDate.length()!=6) {
                        message = message + ResourceUtils.getLocaleString("message.dateerror") + "===";
                        throw new BusinessException(message);
                    }
                }else {
                    Long entrustedLoanHistoryId = dealHistory(dataDate, Constants.BUSINESS_TYPE_ENTRUSTED_LOAN);
                    dealBusinessObject(entrustedLoanHistoryId, new EntrustedLoan(), organization);

                    if (map.size() > 0) {
                        convetEntrustedLoan(map, dataDate, userIp, tempType);
                        message = message + ResourceUtils.getLocaleString("message.26.exportsuccess") + "===";
                    } else {
                        message = message + ResourceUtils.getLocaleString("message.26.noexportdata") + "===";
                    }
                }
            }
            System.out.println("save end:==="+System.currentTimeMillis());
        }else{
            message = message + ResourceUtils.getLocaleString("message.organnull")+"===";
        }
        return message;
    }

    private String convertBZ(String bz){
        if("1".equals(bz)){
            bz="01";
        }
        return  bz;
    }


    private void convetLoan( Map<Object, Map> map, String dataDate, String userIp, String tempType)throws ParseException,BusinessException {

        Set<Loan> loanSet = new HashSet<Loan>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED,tempType, dataDate);
        historyDao.save(history);
        int j=0;
        for (int i = 1; i <= map.size()-1; i++) {
            ++j;
            Loan loanBean = new Loan();
            loanBean.setJrjgbm(String.valueOf(map.get(i).get(3)));
            SysOrganization org=securityService.getOrganizationByOrgCode(loanBean.getJrjgbm());
            if(org!=null){
                loanBean.setOrgId(org.getId());
                loanBean.setJrjgwdmc(org.getOrgName());
                loanBean.setWdssqu(org.getField02());
            }else{
                throw new BusinessException(ResourceUtils.getLocaleString("message.organnull") + "===");
            }

            String errorDesc="";
            loanBean.setZzjgdm(String.valueOf(map.get(i).get(0)));
            loanBean.setQymc(String.valueOf(map.get(i).get(1)));
            loanBean.setSjsj(String.valueOf(map.get(i).get(2)));
            loanBean.setDkhth(String.valueOf(map.get(i).get(6)));
            loanBean.setNbpj(String.valueOf(map.get(i).get(7)));
            loanBean.setWbpj(String.valueOf(map.get(i).get(8)));
            loanBean.setDkjjh(String.valueOf(map.get(i).get(9)));
            String bzDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_BZ,convertBZ(String.valueOf(map.get(i).get(10))),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(bzDesc)){
                errorDesc=errorDesc+"未匹配到关键信息币种;";
            }
            loanBean.setBz(convertBZ(String.valueOf(map.get(i).get(10))));
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(11)).trim())){
                loanBean.setJbll(new BigDecimal(String.valueOf(map.get(i).get(11))));
            }
            if(!StringUtils.isEmpty(  String.valueOf(map.get(i).get(12)).trim())){
                loanBean.setLlfd(new BigDecimal(String.valueOf(map.get(i).get(12))));
            }
            if(!StringUtils.isEmpty(  String.valueOf(map.get(i).get(13)).trim())){
                loanBean.setSjll(new BigDecimal(String.valueOf(map.get(i).get(13))));
            }

            loanBean.setDklbDesc(String.valueOf(map.get(i).get(14)));
            if(!StringUtils.isEmpty(  String.valueOf(map.get(i).get(15)).trim())){
                loanBean.setDkffje(new BigDecimal(String.valueOf(map.get(i).get(15))));
            }
            if(!StringUtils.isEmpty(  String.valueOf(map.get(i).get(16)).trim())){
                loanBean.setDkye(new BigDecimal(String.valueOf(map.get(i).get(16))));
            }
            if(!StringUtils.isEmpty(  String.valueOf(map.get(i).get(17)).trim())){
                loanBean.setFkrq(DateUtils.parseDate(String.valueOf(map.get(i).get(17)),DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty(  String.valueOf(map.get(i).get(18)).trim())){
                loanBean.setDqrq(DateUtils.parseDate(String.valueOf(map.get(i).get(18)),DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(19)).trim())){
                loanBean.setZqrq(DateUtils.parseDate(String.valueOf(map.get(i).get(19)),DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty(String.valueOf(map.get(i).get(20)).trim())){
                String dbfsDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_DBFS,String.valueOf(map.get(i).get(20)),Constants.DICTIONARY_DESC);
                if(StringUtils.isEmpty(dbfsDesc)){
                    errorDesc=errorDesc+"未匹配到关键信息担保方式;";
                    dbfsDesc=" ";
                }
                loanBean.setDbfs(dbfsDesc);
            }
            loanBean.setDbfsCode(String.valueOf(map.get(i).get(20)));
            loanBean.setBzmx(String.valueOf(map.get(i).get(21)));
            if(!StringUtils.isEmpty(String.valueOf(map.get(i).get(22)).trim())){
                loanBean.setBzje(new BigDecimal(String.valueOf(map.get(i).get(22))));
            }
            loanBean.setDymx(String.valueOf(map.get(i).get(23)));
            if(!StringUtils.isEmpty(String.valueOf(map.get(i).get(24)).trim())){
                loanBean.setDyje(new BigDecimal(String.valueOf(map.get(i).get(24))));
            }
            loanBean.setZymx(String.valueOf(map.get(i).get(25)));
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(26)).trim())){
                loanBean.setZyje(new BigDecimal(String.valueOf(map.get(i).get(26))));
            }
            String wjflDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_WJFL,String.valueOf(map.get(i).get(27)),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(wjflDesc)){
                errorDesc=errorDesc+"未匹配到关键信息五级分类;";
            }
            loanBean.setWjfl(String.valueOf(map.get(i).get(27)));
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(28)).trim())){
                loanBean.setQxje(new BigDecimal(String.valueOf(map.get(i).get(28))));
            }
            String dklb=systemDictCacheService.findSysDictionaryCode(Constants.Constants_DKLB,loanBean.getDklbDesc(),Constants.DICTIONARY_CODE);
            if(StringUtils.isEmpty(dklb)){
                errorDesc=errorDesc+"未匹配到关键信息贷款类别;";
            }
            loanBean.setDklb(dklb);
            loanBean.setSource(Constants.SOURCE_IMPORT);
            if(loanBean.getDkye()!=null && loanBean.getDkye().intValue()==0){
                loanBean.setRecordStatus(Constants.RECORD_STATUS_ADDEDSKIPED);
            }else {
                loanBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            }

            loanBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            loanBean.setErrorDesc(errorDesc);
            loanBean.setAdd(true);
            validationService.validLoan(loanBean);
            if(!StringUtils.isEmpty(loanBean.getErrorDesc())){
                loanBean.setExistError(Constants.EXIST_ERROR_YES);
            }else{
                loanBean.setExistError(Constants.EXIST_ERROR_NO);
            }

            loanBean.setHistory(history);
            System.out.println(loanBean.getZzjgdm());
            loanDao.save(loanBean);
            loanSet.add(loanBean);;
            logsService.handleBuzLogData(userIp, "logs.type.23", "logs.operation.import", true, loanBean.getDetail());
        }

        history.setRecordNumber((long) j);
        history.setLoanSet(loanSet);
    }

    private void convetDiscount( Map<Object, Map> map, String dataDate, String userIp, String tempType)throws ParseException {

        Set<Discount> discountSet = new HashSet<Discount>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED, tempType, dataDate);
        historyDao.save(history);
        int j=0;
        for (int i = 1; i <= map.size()-1; i++) {
            ++j;
            Discount discountBean = new Discount();
            String errorDesc="";
            discountBean.setJrjgbm(String.valueOf(map.get(i).get(2)));
            SysOrganization org=securityService.getOrganizationByOrgCode(discountBean.getJrjgbm());
            if(org!=null){
                discountBean.setOrgId(org.getId());
                discountBean.setJrjgwdmc(org.getOrgName());
                discountBean.setWdssqu(org.getField02());
            }else{
                throw new BusinessException(ResourceUtils.getLocaleString("message.organnull") + "===");
            }

            discountBean.setZzjgdm(String.valueOf(map.get(i).get(0)));
            discountBean.setSjsj(String.valueOf(map.get(i).get(1)));
            discountBean.setTxsqrmc(String.valueOf(map.get(i).get(5)));
            String pjzlDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_PJZL,String.valueOf(map.get(i).get(6)),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(pjzlDesc)){
                errorDesc=errorDesc+"未匹配到关键信票据种类;";
            }
            discountBean.setPjzl(String.valueOf(map.get(i).get(6)));
            discountBean.setCdhmc(String.valueOf(map.get(i).get(7)));
            String bzDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_BZ,convertBZ(String.valueOf(map.get(i).get(8))),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(bzDesc)){
                errorDesc=errorDesc+"未匹配到关键信息币种;";
            }
            discountBean.setBz(convertBZ(String.valueOf(map.get(i).get(8))));
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(9)).trim())) {
                discountBean.setTxje(new BigDecimal(String.valueOf(map.get(i).get(9))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(10)).trim())) {
                discountBean.setZsrmb(new BigDecimal(String.valueOf(map.get(i).get(10))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(11)).trim())) {
                discountBean.setZsmy(new BigDecimal(String.valueOf(map.get(i).get(11))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(12)).trim())) {
                discountBean.setTxrq(DateUtils.parseDate(String.valueOf(map.get(i).get(12)), DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(13)).trim())) {
                discountBean.setCdrq(DateUtils.parseDate(String.valueOf(map.get(i).get(13)), DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(14)).trim())) {
                discountBean.setTxll(new BigDecimal(String.valueOf(map.get(i).get(14))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(15)).trim())) {
                discountBean.setPmje(new BigDecimal(String.valueOf(map.get(i).get(15))));
            }
            String wjflDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_WJFL,String.valueOf(map.get(i).get(16)),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(wjflDesc)){
                errorDesc=errorDesc+"未匹配到关键信息五级分类;";
            }
            discountBean.setWjfl(String.valueOf(map.get(i).get(16)));
            discountBean.setCdhdq(String.valueOf(map.get(i).get(17)));

            discountBean.setSource(Constants.SOURCE_IMPORT);
            discountBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            discountBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            discountBean.setErrorDesc(errorDesc);
            discountBean.setAdd(true);
            validationService.validDiscount(discountBean);
            if(!StringUtils.isEmpty(discountBean.getErrorDesc())){
                discountBean.setExistError(Constants.EXIST_ERROR_YES);
            }else{
                discountBean.setExistError(Constants.EXIST_ERROR_NO);
            }

            discountBean.setHistory(history);

            discountDao.save(discountBean);
            discountSet.add(discountBean);

            logsService.handleBuzLogData(userIp,"logs.type.24","logs.operation.import",true,discountBean.getDetail());
        }
        history.setRecordNumber((long) j);
        history.setDiscountSet(discountSet);

    }

    private void convetDrafts( Map<Object, Map> map, String dataDate, String userIp, String tempType)throws ParseException {

        Set<Drafts> draftsSet = new HashSet<Drafts>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED,tempType, dataDate);
        historyDao.save(history);
        int j=0;
        for (int i = 1; i <= map.size()-1; i++) {
            ++j;
            Drafts draftsBean = new Drafts();
            String errorDesc="";

            SysOrganization org=securityService.getOrganizationByOrgCode(draftsBean.getJrjgbm());
            if(org!=null){
                draftsBean.setOrgId(org.getId());
            }else{
                throw new BusinessException(ResourceUtils.getLocaleString("message.organnull") + "===");
            }

            draftsBean.setZzjgdm(String.valueOf(map.get(i).get(0)));
            draftsBean.setSjsj(String.valueOf(map.get(i).get(1)));
            draftsBean.setCprmc(String.valueOf(map.get(i).get(2)));
            String bzDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_BZ,convertBZ(String.valueOf(map.get(i).get(4))),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(bzDesc)){
                errorDesc=errorDesc+"未匹配到关键信息币种;";
            }
            draftsBean.setBz(convertBZ(String.valueOf(map.get(i).get(4))));
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(5)).trim())){
                draftsBean.setHpje(new BigDecimal(String.valueOf(String.valueOf(map.get(i).get(5)))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(6)).trim())) {
                draftsBean.setHpcdr(DateUtils.parseDate(String.valueOf(map.get(i).get(6)), DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(7)).trim())) {
                draftsBean.setHpdqr(DateUtils.parseDate(String.valueOf(map.get(i).get(7)), DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(8)).trim())) {
                draftsBean.setHpfkrq(DateUtils.parseDate(String.valueOf(map.get(i).get(8)), DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(9)).trim())){
                draftsBean.setBzjbl(new BigDecimal(String.valueOf(String.valueOf(map.get(i).get(9)))));
            }
            String dbbzDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_DBFS,String.valueOf(map.get(i).get(10)),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(dbbzDesc)){
                errorDesc=errorDesc+"未匹配到关键信息担保标志;";
            }
            draftsBean.setDbbz(String.valueOf(map.get(i).get(10)));
            String dkbzDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_DKBZ,String.valueOf(map.get(i).get(11)),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(dkbzDesc)){
                errorDesc=errorDesc+"未匹配到关键信息贷款标志;";
            }
            draftsBean.setDkbz(String.valueOf(map.get(i).get(11)));
            String wjflDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_WJFL,String.valueOf(map.get(i).get(12)),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(wjflDesc)){
                errorDesc=errorDesc+"未匹配到关键信息五级分类;";
            }
            draftsBean.setWjfl(String.valueOf(map.get(i).get(12)));

            draftsBean.setSource(Constants.SOURCE_IMPORT);
            draftsBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            draftsBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            draftsBean.setErrorDesc(errorDesc);
            draftsBean.setAdd(true);
            validationService.validDrafts(draftsBean);
            if(!StringUtils.isEmpty(draftsBean.getErrorDesc())){
                draftsBean.setExistError(Constants.EXIST_ERROR_YES);
            }else{
                draftsBean.setExistError(Constants.EXIST_ERROR_NO);
            }

            draftsBean.setHistory(history);

            draftsDao.save(draftsBean);
            draftsSet.add(draftsBean);

            logsService.handleBuzLogData(userIp,"logs.type.25","logs.operation.import",true,draftsBean.getDetail());
        }

        history.setRecordNumber((long) j);
        history.setDraftsdSet(draftsSet);

    }

    private void convetEntrustedLoan( Map<Object, Map> map, String dataDate, String userIp, String tempType)throws ParseException {

        Set<EntrustedLoan> entrustedLoanSet = new HashSet<EntrustedLoan>(0);
        History history = generateHistory(Constants.RECORD_STATUS_ADDED,tempType, dataDate);
        historyDao.save(history);
        int j=0;
        for (int i = 1; i <= map.size()-1; i++) {
            ++j;
            EntrustedLoan entrustedLoanBean = new EntrustedLoan();
            String errorDesc="";
            entrustedLoanBean.setJrjgbm(String.valueOf(map.get(i).get(1)));
            SysOrganization org=securityService.getOrganizationByOrgCode(entrustedLoanBean.getJrjgbm());
            if(org!=null){
                entrustedLoanBean.setOrgId(org.getId());
            }else{
                throw new BusinessException(ResourceUtils.getLocaleString("message.organnull") + "===");
            }

            entrustedLoanBean.setSjsj(String.valueOf(map.get(i).get(0)));
            entrustedLoanBean.setWtrzzjgdm(String.valueOf(map.get(i).get(2)));
            entrustedLoanBean.setJkrzzjgdm(String.valueOf(map.get(i).get(3)));
            String bzDesc=systemDictCacheService.findSysDictionaryCode(Constants.Constants_BZ,convertBZ(String.valueOf(map.get(i).get(4))),Constants.DICTIONARY_DESC);
            if(StringUtils.isEmpty(bzDesc)){
                errorDesc=errorDesc+"未匹配到关键信息币种;";
            }
            entrustedLoanBean.setBz(convertBZ(String.valueOf(map.get(i).get(4))));
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(5)).trim())) {
                entrustedLoanBean.setLl(new BigDecimal(String.valueOf(String.valueOf(map.get(i).get(5)))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(6)).trim())) {
                entrustedLoanBean.setYwfse(new BigDecimal(String.valueOf(String.valueOf(map.get(i).get(6)))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(7)).trim())) {
                entrustedLoanBean.setYwye(new BigDecimal(String.valueOf(String.valueOf(map.get(i).get(7)))));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(8)).trim())) {
                entrustedLoanBean.setDkffrq(DateUtils.parseDate(String.valueOf(map.get(i).get(8)), DateUtils.DATE_FORMAT_YYYYMMdd));
            }
            if(!StringUtils.isEmpty( String.valueOf(map.get(i).get(9)).trim())) {
                entrustedLoanBean.setDkdqrq(DateUtils.parseDate(String.valueOf(map.get(i).get(9)), DateUtils.DATE_FORMAT_YYYYMMdd));
            }

            entrustedLoanBean.setSource(Constants.SOURCE_IMPORT);
            entrustedLoanBean.setRecordStatus(Constants.RECORD_STATUS_ADDED);
            entrustedLoanBean.setReportStatus(Constants.REPORT_STATUS_ACTIVE);
            entrustedLoanBean.setErrorDesc(errorDesc);
            entrustedLoanBean.setAdd(true);
            validationService.validEntrustedLoan(entrustedLoanBean);
            if(!StringUtils.isEmpty(entrustedLoanBean.getErrorDesc())){
                entrustedLoanBean.setExistError(Constants.EXIST_ERROR_YES);
            }else{
                entrustedLoanBean.setExistError(Constants.EXIST_ERROR_NO);
            }
            entrustedLoanBean.setHistory(history);

            entrustedLoanDao.save(entrustedLoanBean);
            entrustedLoanSet.add(entrustedLoanBean);

            logsService.handleBuzLogData(userIp, "logs.type.26", "logs.operation.import",true,entrustedLoanBean.getDetail());
        }

        history.setRecordNumber((long) j);
        history.setEntrustedLoanSet(entrustedLoanSet);

    }
}
